package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.InfoCardContractGoodsDetailBean;
import com.wwwarehouse.contract.bean.InfoCardOrderDetailBean;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/11/3.
 */

public class InfoCardOrderDetailAdapter extends RecyclerView.Adapter<InfoCardOrderDetailAdapter.OrderDetailViewHolder> {
	private List<InfoCardContractGoodsDetailBean.ItemDetailBean> mList = new ArrayList<>();
	private Context mContext;
	private int mHeight;
	private OnItemClickListener mOnItemClickListener;

	public InfoCardOrderDetailAdapter(List<InfoCardContractGoodsDetailBean.ItemDetailBean> mList, Context mContext, int mHeight) {
		this.mList = mList;
		this.mContext = mContext;
		this.mHeight = mHeight;
	}

	public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		this.mOnItemClickListener = mOnItemClickListener;
	}

	@Override
	public OrderDetailViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		OrderDetailViewHolder holder = new OrderDetailViewHolder(LayoutInflater.from(mContext).inflate(R.layout.contract_info_card_item_order_details, null, false));
		return holder;
	}

	@Override
	public void onBindViewHolder(OrderDetailViewHolder holder, final int position) {
		RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.mContentLayout.getLayoutParams();
		layoutParams.height = mHeight / 7 + 10;
		if (mList == null || mList.isEmpty()) {
			return;
		}
		holder.mContentLayout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mOnItemClickListener.OnItemClick(position);
			}
		});
		if (StringUtils.isNoneNullString(mList.get(position).getPbPrice())) {
			holder.mMoneyTv.setText(StringUtils.getResourceStr(mContext , R.string.string_contract_money_price , mList.get(position).getPbPrice()));
		}
		if (StringUtils.isNoneNullString(mList.get(position).getName())) {
			holder.mNameTv.setText(mList.get(position).getName());
		}
		if (StringUtils.isNoneNullString(mList.get(position).getPbQty())) {
			if (StringUtils.isNoneNullString(mList.get(position).getUnit())) {
				holder.mCountTv.setText("x" + mList.get(position).getPbQty() + mList.get(position).getUnit());
			} else {
				holder.mCountTv.setText("x" + mList.get(position).getPbQty());
			}
		}
		if (StringUtils.isNoneNullString(mList.get(position).getImg())) {
			ImageloaderUtils.displayRoundImg(mList.get(position).getImg() , holder.mLogoIv);
		}
	}

	@Override
	public int getItemCount() {
		return mList.size();
	}


	public class OrderDetailViewHolder extends RecyclerView.ViewHolder {
		private TextView mNameTv;
		private ImageView mLogoIv;
		private TextView mMoneyTv;
		private TextView mCountTv;
		private LinearLayout mContentLayout;

		public OrderDetailViewHolder(View itemView) {
			super(itemView);
			mNameTv = (TextView) itemView.findViewById(R.id.name_tv);
			mLogoIv = (ImageView) itemView.findViewById(R.id.logo_iv);
			mMoneyTv = (TextView) itemView.findViewById(R.id.money_tv);
			mCountTv = (TextView) itemView.findViewById(R.id.count_tv);
			mContentLayout = (LinearLayout) itemView.findViewById(R.id.content_layout);
		}
	}

	public interface OnItemClickListener {
		void OnItemClick(int position);
	}
}
